﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class CableTrayDimForm : System.Windows.Forms.Form
	{
		public CableTrayDimForm()
		{
			this.InitializeComponent();
		}

		public LeadStyle LeadStyle
		{
			get
			{
				if (this.radioCrossLine.Checked)
				{
					return LeadStyle.Orthogonal;
				}
				if (this.radioLine.Checked)
				{
					return LeadStyle.HasLead;
				}
				return LeadStyle.None;
			}
		}

		public LeadSymbol LeadSymbol
		{
			get
			{
				if (this.radioDot.Checked)
				{
					return LeadSymbol.Dot;
				}
				if (this.radioArrow.Checked)
				{
					return LeadSymbol.Arrow;
				}
				return LeadSymbol.None;
			}
		}

		private void CableTrayDimForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.ReadFormDataFromXml();
			this.RefreshSymbolRadio();
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("CableTrayDim");
			int num = Convert.ToInt32(xmlHelper.GetAttributeEx("Line", "1"));
			if (num == 0)
			{
				this.radioNoLine.Checked = true;
			}
			else if (num == 1)
			{
				this.radioLine.Checked = true;
			}
			else
			{
				this.radioCrossLine.Checked = true;
			}
			num = Convert.ToInt32(xmlHelper.GetAttributeEx("Symbol", "0"));
			if (num == 0)
			{
				this.radioNone.Checked = true;
			}
			else if (num == 1)
			{
				this.radioArrow.Checked = true;
			}
			else
			{
				this.radioDot.Checked = true;
			}
			int x = Convert.ToInt32(xmlHelper.GetAttributeEx("X", "100"));
			int y = Convert.ToInt32(xmlHelper.GetAttributeEx("Y", "100"));
			base.Location = new System.Drawing.Point(x, y);
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("CableTrayDim");
			xmlHelper.SetAttribute("Line", ((int)this.LeadStyle).ToString());
			xmlHelper.SetAttribute("Symbol", ((int)this.LeadSymbol).ToString());
			xmlHelper.SetAttribute("X", base.Location.X.ToString());
			xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			xmlHelper.SaveXml("");
		}

		private void RefreshSymbolRadio()
		{
			if (this.radioNoLine.Checked)
			{
				this.radioNone.Enabled = false;
				this.radioArrow.Enabled = false;
				this.radioDot.Enabled = true;
				return;
			}
			this.radioNone.Enabled = true;
			this.radioArrow.Enabled = true;
			this.radioDot.Enabled = true;
		}

		private void radioNoLine_Click(object sender, EventArgs e)
		{
			this.RefreshSymbolRadio();
		}

		private void CableTrayDimForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveFormDataToXml();
		}

		protected override void WndProc(ref Message msg)
		{
			if (msg.Msg == 274 && (int)msg.WParam == 61536)
			{
				base.Hide();
				return;
			}
			base.WndProc(ref msg);
		}

		private const string Xml_Node_Root = "CableTrayDim";

		private const string Xml_Att_Line = "Line";

		private const string Xml_Att_Symbol = "Symbol";

		private const string LocationX = "X";

		private const string LocationY = "Y";
	}
}
